// add by yanyj 20180727 start
// 验证框架

class  Validation {

    static METHODS = {
        // 必填
        required: function (value) {
            return !pocket.isUndefined(value) && value.trim() !== "";
        },
        // 是某种类型
        is: function (isValue) {
            return function (value) {
                return pocket.is(value, isValue);
            }
        },
        // 指定长度
        length: function(length) {
            return function (value) {
                return !!value.length && value.length === length;
            }
        },
        // 相等（简单数据类型）
        equal: function (equalValue) {
            return function (value) {
                return equalValue === value;
            }
        },

        /**
         *  在两数之间
         * @param {number} start 起始值
         * @param {number}end 终止值
         * @param {boolean}startInclude 起始值包含
         * @param {boolean}endInclude 终止值包含
         * @returns {function(*): boolean} 返回结果
         */
        between: function (start, end, startInclude = true, endInclude = true) {
            return function (value) {
                return (startInclude ? value >= start : value > start) && (endInclude ? value <= end : value < end);
            }
        },

        // 数组包含
        include: function(array) {
            return value =>  array.includes(value);
        },

        // 对象
        hasKeys: function (keys = []) {
            return value => {
                let _keys = pocket.keys(value);
                for(let i =0, length = keys.length; i < length; i++) {
                    if(!_keys.includes(keys[i])) {
                        return false;
                    }
                }

                return true;
            }
        },

        hasValues: function (obj = {}) {
            return value => {
                let keys = Object.keys(obj);
                // let _keys = Object.keys(value);
                for(let i =0, length = keys.length; i < length; i++) {
                    let key = keys[i];
                    if(value[key] !== obj[key]) {
                        return false;
                    }
                }

                return true;
            }
        }


    };

    static warnMethods = {
        tooltip: function () {
            if(!$.prototype.tooltip) {
                throw new Error('Validation [Error] $.prototype,tooltip 未实现，请导入bootstrap和popper库')
            }
            return function (message, name) {
                let $tooltipTarget = $(`[name=${name}]`);
                $tooltipTarget.tooltip({placement:"top",title: message}).tooltip('enable').tooltip('show');

                $tooltipTarget.on('hidden.bs.tooltip', function () {
                    $tooltipTarget.tooltip('disable');
                    $tooltipTarget.un('hidden.bs.tooltip')
                })
            };
        },
        console: function () {
            return console.warn;
        }
    };

    constructor(validation = {}){
        this.rules = validation.rules;
        this.messages = validation.messages;
        this.validMethod = this.getValidMethod(validation.validMethod || 'layer');
    }

    // 通用验证方法
     valid(data) {
        let keys = pocket.keys(data),
            _this = this;

        for(let i = 0, length = keys.length; i < length; i++) {
            let ruleKey = keys[i];
            let keyRules = this.rules[ruleKey]; // 获取rules
            if(!keyRules) continue;
            let methodsKeys = pocket.keys(keyRules);
            for(let j = 0; j < methodsKeys.length; j++) {
                let methodKey = methodsKeys[j];
                if( !keyRules[methodKey].call(null, data[ruleKey])) {
                    _this.validMethod(_this.messages[ruleKey][methodKey], ruleKey);
                    return false;
                }
            }

        }
        return true;
     }

     getValidMethod(type) {

        if(Validation.warnMethods[type]) {
            return Validation.warnMethods[type]();
        } else {
            return Validation.warn.bind(LayerTip);
        }
     }

     extendValidMethod(name, method) {
         Validation.warnMethods[name] = function () {
            return method;
        };

        return this;
     }
}


// add by yanyj 20180727 end